package com.vy.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vy.stl.constants.Constants;
import com.vy.stl.entity.Admin;
import com.vy.stl.mapper.AdminMapper;
import com.vy.stl.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * @create: 2025/08/21 15:46
 * @description:
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin getAdminByPhone(String phone) {
        return adminMapper.selectAdminByPhone(phone);
    }

    @Override
    public List<Admin> search(Admin admin) {
        return adminMapper.select(admin);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Admin admin) {
        //页码参数为空则默认设置为第1页
        if(ObjectUtils.isEmpty(page)) page = 1;
        //每页条数参数为空则默认设置为10条/页
        if(ObjectUtils.isEmpty(limit)) limit = 10;
        //开启分页功能，接下来的第一个MyBatis查询（SELECT语句）会被自动拦截并处理为分页查询，查询结果会包含分页信息
        PageHelper.startPage(page, limit);
        List<Admin> admins = adminMapper.select(admin);
        //使用查询到的结果列表admins创建PageInfo对象
        PageInfo<Admin> pageInfo = new PageInfo<>(admins);
        return pageInfo;
    }

    @Override
    public int  addAdmin(Admin admin) {
        // 查询手机号是否被占用
        Admin dbAdmin = adminMapper.selectAdminByPhone(admin.getPhone());
        if (!ObjectUtils.isEmpty(dbAdmin)) {
            //手机号被占用，不能使用
            return Constants.PHONE_HAS_ERROR;
        }
        // 添加管理员信息
        // 添加管理之前，要对密码进行m5d加密
        String md5Password = getMd5Password(admin.getPassword(), admin.getPhone());
        admin.setPassword(md5Password);
        return adminMapper.insert(admin);
    }

    @Override
    public int modifyAdmin(Admin admin) throws Exception {
        Admin dbadmin = adminMapper.selectAdminByPhone(admin.getPhone());
        // 检查手机号是否已被其他人注册，如果手机号查询管理员存在并且id不一致，则不是一个人
        if (!ObjectUtils.isEmpty(dbadmin) && admin.getId() != dbadmin.getId()) {
            //手机号存在
            throw new Exception("手机号已经存在");
        }
        //修改用户信息
        if(StringUtils.hasText(admin.getPassword())){
            String md5Password = getMd5Password(admin.getPassword(), admin.getPhone());
            admin.setPassword(md5Password);
        }
        int row = adminMapper.editAdmin(admin);
        return row;
    }

    @Override
    public Admin login(Admin admin) throws Exception {
        // 判断账号, 密码格式是否正确
        if (admin.getPhone() == null || admin.getPhone().length() <3 || admin.getPhone().length() > 16) {
            throw new Exception("账号格式不正确");
        }
        if (admin.getPassword() == null || admin.getPassword().length() <3 || admin.getPassword().length() > 16) {
            throw new Exception("密码格式不正确");
        }
        // 1. 先根据手机号是否存在
        Admin dbAdmin = adminMapper.selectAdminByPhone(admin.getPhone());
        if (ObjectUtils.isEmpty(dbAdmin)) {
            throw new Exception("手机号或密码错误");
        }
        //2. 输入密码是明文，数据里存的是加密之后的密码。
        //2.1 思路 对明文密码进行加密，加密后再跟数据库的密码进行比较
        String md5Password = getMd5Password(admin.getPassword(), admin.getPhone());
        if (!md5Password.equals(dbAdmin.getPassword())) {
            throw  new Exception("手机号或密码错误");
        }
        return dbAdmin;
    }

    private String getMd5Password(String password, String salt) {
        for (int i = 0; i < 3; i++) {
            String newPass = salt + password + salt;
            password = DigestUtils.md5DigestAsHex(newPass.getBytes()).toUpperCase();
        }
        return password;
    }
}
